package cn.edu.neu.vmmigeval.cloud.policy.impl;


import cn.edu.neu.vmmigeval.cloud.entity.Host;
import cn.edu.neu.vmmigeval.cloud.entity.Vm;
import cn.edu.neu.vmmigeval.cloud.policy.MigratedHostPolicy;
import cn.edu.neu.vmmigeval.cloud.policy.VmSelectionPolicy;

import java.util.List;

/**
 * The Minimum Utilization (MU) Broker selection policy.
 * @author zzh
 *
 */
public class VmSelectionPolicyMinimumUtilization extends VmSelectionPolicy {

	public VmSelectionPolicyMinimumUtilization(
			MigratedHostPolicy migratedHostPolicy) {
		super(migratedHostPolicy);
	}

	public VmSelectionPolicyMinimumUtilization(){
		
	}
	@Override
	public Vm getVmToMigrate(Host host) {
		List<Vm> migratableVms = getMigratableVms(host);
		if (migratableVms.isEmpty()) {
			return null;
		}
		Vm vmToMigrate = null;
		double minMetric = 0.5;// Double.MAX_VALUE;
		for (Vm vm : migratableVms) {
			if (vm.isInMigration()) {
				continue;
			}
			double metric = (double)vm.getCurrentCpu() / vm.getTotalCpu();
			//double metric = vm.getTotalUtilizationOfCpuMips(CloudSim.clock()) / vm.getMips();
			if (metric < minMetric) {
				minMetric = metric;
				vmToMigrate = vm;
			}
		}
		return vmToMigrate;
	}

}
